package com.babyshop.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.babyshop.domain.User;
import com.googlecode.s2hibernate.struts2.plugin.annotations.SessionTarget;
import com.googlecode.s2hibernate.struts2.plugin.annotations.TransactionTarget;

public class UserDaoImpl implements UserDao {

	@SessionTarget
	Session session;

	@TransactionTarget
	Transaction transaction;

	@Override
	public void saveOfUpdate(User user) {
		try {
			session.saveOrUpdate(user);
		} catch (Exception e) {
			transaction.rollback();
			e.printStackTrace();
		}

	}

	@Override
	public void delete(int id) {
		try {
			User user = (User) session.get(User.class, id);
			session.delete(user);
		} catch (Exception e) {
			transaction.rollback();
			e.printStackTrace();
		}

	}

	@Override
	public User get(int id) {
		User user = null;
		try {
			user = (User) session.get(User.class, id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return user;
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<User> getAll() {
		List<User> listUsers = null;
		try {
			listUsers = session.createQuery("from User").list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return listUsers;
	}
	
	

}
